﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Gakita.Model.Enemies
{
    /* This class handles the logic for the SpikyThing enemy. SpikyThings can be of three different variations: Horizontal, Vertical or             Freefalling. Horizontal and Vertical moves a certain distance in one direction, then turns around. The FreeFalling ones are spawned by       the FlyingEnemy and just falls off the screen */
    class SpikyThing
    {
        public enum Direction
        {
            Horizontal,
            Vertical,
            FreeFalling
        }

        private static Random rng                   =       new Random();

        private const float _distanceToTravel       =       2.5f;

        private Direction _direction;
        private Vector2 _centerPosition;
        private Vector2 _startPosition;
        private Vector2 _speed;

        public Vector2 _size                        =       new Vector2(0.30f, 0.30f);

        public SpikyThing(Vector2 aCenterPosition, Direction aDirection)
        {
            _direction = aDirection;
            _centerPosition = aCenterPosition;

            // Depending on the type, different starting values are assigned.
            if (_direction == Direction.Vertical)
            {
                _centerPosition.Y -= (float)rng.NextDouble() * _distanceToTravel;
                _startPosition = aCenterPosition;
                _speed = new Vector2(0, -3);
            }
            else if (_direction == Direction.Horizontal)
            {
                _centerPosition.X += (float)rng.NextDouble() * _distanceToTravel;
                _startPosition = aCenterPosition;
                _speed = new Vector2(3, 0);
            }
            else
            {
                _speed = new Vector2(0, 5);
            }
            
        }

        public void Update(float aElapsedTime)
        {
            // If the SpikyThing isn't Freefalling, we have to check if it is time for it to turn around.
            if (_direction != Direction.FreeFalling)
            {
                Vector2 nextPosition = _centerPosition + _speed * aElapsedTime;

                if (ItIsTimeToTurnAround(nextPosition))
                {
                    TurnAround();
                }
            }

            _centerPosition += _speed * aElapsedTime;
            
        }

        /* This method turns the SpikyThing around by reversing its' speed */
        private void TurnAround()
        {
            _speed = -_speed;
        }

        /* This method checks if the spiky thing have moved a specific distance from it's starting position, 
           in which case it should turn around */
        private bool ItIsTimeToTurnAround(Vector2 aNextPosition)
        {
            if (_direction == Direction.Vertical)
            {
                if (aNextPosition.Y > _startPosition.Y + _size.Y / 2 || aNextPosition.Y < _startPosition.Y - _distanceToTravel - _size.Y / 2)
                {
                    return true;
                }
            }
            else
            {
                if (aNextPosition.X < _startPosition.X - _size.X / 2 || aNextPosition.X > _startPosition.X + _distanceToTravel + _size.X / 2)
                {
                    return true;
                }
            }
            return false;
        }

        public Vector2 GetPosition()
        {
            return _centerPosition;
        }

    }
}
